package com.ukao.cashregister.dialog;


import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.listener.MyTextWatcher;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.SystemUIUtils;
import com.ukao.cashregister.widget.ClearEditText;
import com.ukao.cashregister.widget.StateImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.cashregister.R.id.tansno_txt;

/**
 * 添加运单号对话框
 */
public class TansNoDialogFragment extends BaseDialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.edit_name_cancel)
    Button editNameCancel;
    @BindView(R.id.edit_name_submit)
    Button editNameSubmit;
    Unbinder unbinder;
    @BindView(tansno_txt)
    ClearEditText tansnoTxt;

    // TODO: Rename and change types of parameters
    private String titles;
    private String tansNo;




    public static TansNoDialogFragment newInstance(String param1, String param2) {
        TansNoDialogFragment fragment = new TansNoDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            titles = getArguments().getString(ARG_PARAM1);
            tansNo = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tans_no_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        init();
        initListener();
    }

    @Override
    protected void initListener() {
        super.initListener();

        tansnoTxt.setOnEditorActionListener(mOnEditorActionListener);

    }
    private TextView.OnEditorActionListener mOnEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if (!CheckUtils.isNull(event) && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    || actionId == EditorInfo.IME_ACTION_DONE||actionId == EditorInfo.IME_ACTION_NEXT) {
           submitNo();
            }

            return false;
        }
    };

    private void init() {
        title.setText(titles);
        String  filltansNo=CheckUtils.isEmptyString(tansNo);
        tansnoTxt.setText(filltansNo);
        tansnoTxt.setSelection(filltansNo.length());
    }






    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.close_btn, R.id.edit_name_cancel, R.id.edit_name_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.edit_name_cancel:
                dismiss();
                break;
            case R.id.edit_name_submit:
                submitNo();

                break;
        }
    }

    public void submitNo() {
        String mtansnoTxt = getText(tansnoTxt);
        if (!mtansnoTxt.isEmpty()){
            if (mOnDialogSureClickListener!=null){
                mOnDialogSureClickListener.onSureItemClick(mtansnoTxt);
            }
        }
        dismiss();
    }


    public static Builder createBuilder(FragmentManager fragmentManager) {
        return new Builder(fragmentManager);
    }


    public static final class Builder {
        private String title="";
        private  String  tansNo="";
        private OnDialogSureClickListener mOnDialogSureClickListener;
        private  FragmentManager mFragmentManager;
        public Builder(FragmentManager fragmentManager) {
            this.mFragmentManager=fragmentManager;
        }

        public Builder title(String val) {
            title = val;
            return this;
        }

        public Builder tansNo(String val) {
            tansNo = val;
            return this;
        }
        public Builder mOnDialogSureClickListener(OnDialogSureClickListener val) {
            mOnDialogSureClickListener = val;
            return this;
        }

        public TansNoDialogFragment show() {
            TansNoDialogFragment mTansNoDialog = TansNoDialogFragment.newInstance(title, tansNo);
            mTansNoDialog.setOnSureClickListener(mOnDialogSureClickListener);
            mTansNoDialog.show(mFragmentManager, SelectNumDialogFragment.class.getName());
            return mTansNoDialog;
        }
    }
}
